package com.jf.anmocker.plugin.mockcore.asm

import org.objectweb.asm.ClassVisitor

class AsmMockerCache {

    private val clsVisitorMap = HashMap<String, ClassVisitor>()
    private val targetClzMarkPool = HashMap<String, ClassVisitor>()
    private val methodVisitorMap = HashMap<String, AnMockerMethodVisitor>()

    companion object {
        fun getInstance() = InstanceHolder.instance
    }

    object InstanceHolder {
        val instance = AsmMockerCache()
    }

    fun saveClassVisitor(key: String?, clzVisitor: ClassVisitor) {
        if(key != null && key.isNotEmpty()){
            clsVisitorMap[key] = clzVisitor
            println("saveClassVisitor for $key success")
        }else{
            println("saveClassVisitor failed: key is empty")
        }
    }

    fun getClassVisitor(key: String) : ClassVisitor? {
        return clsVisitorMap[key]
    }

    fun markTargetClass(key: String, clzMocker: ClassVisitor){
        targetClzMarkPool[key] = clzMocker
    }

    fun isTargetClass(key: String) : Boolean{
        return targetClzMarkPool.contains(key.replace("/","."))
    }

    fun getClassMocker(key: String) : AnMockerClzNode?{
        val visitor = targetClzMarkPool[key.replace("/",".")]
        return if(visitor is AnMockerClzNode){
            visitor
        }else{
            null
        }
    }

    fun removeTargetMark(key: String){
        targetClzMarkPool.remove(key)
    }

    fun destroy(){
        targetClzMarkPool.clear()
        clsVisitorMap.clear()
    }
}